import { Component, OnInit } from '@angular/core';
import { HostConfigService } from '../../../service/config.service';
import { NotifyService } from '../../../service/notify.service';

@Component({
  selector: 'app-history',
  templateUrl: './history.component.html',
  styleUrls: ['./history.component.css']
})
export class HistoryComponent implements OnInit {

  historyList: any;
  constructor(private hostConfig: HostConfigService, private notify: NotifyService) { }

  ngOnInit(): void {
    this.refresh();
  }
  selectedIndex = -1;
  click(index) {
    this.selectedIndex = index;
  }
  openHistory(history) {
    this.notify.emitOpenTab({ type: history.optype, host: history.host });
  }
  deleteHistory(history) {
    this.hostConfig.deleteHistory(history.optype, history.host);
    this.refresh();
  }
  refresh() {
    this.historyList = this.hostConfig.getHistory();
  }
  titleSortFn = (a: any, b: any) => a.host.title.localeCompare(b.host.title);
  ipSortFn = (a: any, b: any) => a.host.ip.localeCompare(b.host.ip);
  operSortFn = (a: any, b: any) => a.optype.localeCompare(b.optype);
  timeSortFn = (a: any, b: any) => a.host.oper[a.optype].localeCompare(b.host.oper[b.optype]);
}
